home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / System / ScreenDaemon 1.2 / ScreenDaemon cdev / Framework / CDEVMain.c < prev    next >
C/C++ Source or Header  |  1995-05-16  |  3KB  |  95 lines

  1. // ©1994-95, Matthew E. Axsom, All Rights Reserved
  2. #include <A4Stuff.h>
  3. #include <Devices.h>
  4.  
  5. #ifndef    _H_TControlPanel
  6. #include "TControlPanel.h"
  7. #endif
  8.  
  9. // to be declared in your cdev code (see example CDEV.cp file)
  10. extern long    runable(void);
  11. extern TControlPanel *makeCDEV(short numItems,DialogPtr cp);
  12.  
  13. // main entry point for control panel
  14. pascal long main(short message,short item,short numItems,short /*privateValue*/,
  15.                  EventRecord *e, TControlPanel *cdev, DialogPtr d)
  16. {
  17.     // set up a4 so we can access the globals
  18.     long oldA4=SetCurrentA4();
  19.     
  20.     // return code
  21.     long     result=0;
  22.     
  23.     switch (message) {
  24.         // do initialization
  25.         case initDev:
  26.             if ((long)cdev == cdevUnset) {
  27.                 cdev = makeCDEV(numItems,d);
  28.                 cdev->Init();        // call init method
  29.             }
  30.             break;
  31.                 
  32.         // control panel is closing
  33.         case closeDev:
  34.             result=cdevUnset;
  35.             break;
  36.         
  37.         // can we run? return 1 if so, else 0
  38.         case macDev:
  39.             result=runable();
  40.             break;
  41.         
  42.         // it's not an init, open, or close message
  43.         default:
  44.             if ((long)cdev != cdevUnset) {
  45.                 // copy over this event and call the action proc
  46.                 cdev->fEvent = e;            
  47.                 result=cdev->actions(message,item);
  48.             }
  49.             break;
  50.     }
  51.     
  52.     // If this is macDev, then we do not need any additional error checking.  The result
  53.     // from runable is just what we need.
  54.     if (message != macDev) {
  55.         // if there is no error code then make sure we return
  56.         // the cdev as the result, as per IM:MMT pg 8-30
  57.         if (result == 0)
  58.             result = (long)cdev;
  59.         else {
  60.             // there's either an err, or we are closing.  In either case, delete the cdev
  61.             cdev->Close();    // call close routine.
  62.             delete cdev;    // delete the object
  63.             
  64.             // if we are deleting then result is cdevUnset and there is no err.  Otherwise it
  65.             // must be an error code returned by one of the functions.
  66.             
  67.             // make sure the error message conforms to one of the standard return values.
  68.             // If the message doesn't match one of the standard ones, return a
  69.             // generic error that simply closes the cdev w/no message to the user.
  70.             // See NIM:More Mac Toolbox pg 8-47
  71.             switch (result) {
  72.                 case cdevGenErr:
  73.                 case cdevResErr:
  74.                 case cdevUnset:
  75.                     break;
  76.                 
  77.                 // translate internal out-of-memory code to OS acceptable
  78.                 // out-of-memory code.  See define of cdevFWMemErr for additional
  79.                 // explanation.
  80.                 case cdevFWMemErr:
  81.                     result=cdevMemErr;
  82.                     break;
  83.                     
  84.                 default:
  85.                     result=cdevGenErr;
  86.                     break;
  87.             }
  88.         }
  89.     }
  90.     
  91.     // restore a4 before exiting
  92.     SetA4(oldA4);
  93.         
  94.     return(result);
  95. }